package com.library.librarybackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.librarybackend.domain.OSSObject;
import com.library.librarybackend.domain.dto.AttachmentUploadDTO;
import com.library.librarybackend.domain.dto.AttachmentUploadsDTO;
import com.library.librarybackend.domain.entity.Attachment;
import com.library.librarybackend.domain.vo.AttachmentVO;
import com.library.librarybackend.service.AttachmentService;
import com.library.librarybackend.mapper.AttachmentMapper;
import com.library.librarybackend.service.OSSService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author MR
 * @description 针对表【attachment(附件表)】的数据库操作Service实现
 * @createDate 2025-09-21 14:28:03
 */
@Service
@RequiredArgsConstructor
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment>
        implements AttachmentService {

    private final OSSService ossService;

    @Override
    public AttachmentVO upload(AttachmentUploadDTO attachmentUploadDTO) throws IOException {
        // 上传文件
        OSSObject upload = ossService.upload(attachmentUploadDTO.getFile().getInputStream(),
                attachmentUploadDTO.getFile().getContentType(),
                attachmentUploadDTO.getFile().getOriginalFilename(),
                attachmentUploadDTO.getPath(),
                attachmentUploadDTO.getIsPrivate());
        // 添加附件数据
        Attachment attachment = BeanUtil.copyProperties(upload, Attachment.class);
        attachment.setOwnerType(attachmentUploadDTO.getOwnerType());
        attachment.setOwnerId(attachmentUploadDTO.getOwnerId());
        save(attachment);
        return BeanUtil.copyProperties(attachment, AttachmentVO.class);
    }

    @Override
    public List<AttachmentVO> uploads(AttachmentUploadsDTO attachmentUploadsDTO) {
        return attachmentUploadsDTO.getFiles()
                .stream()
                .map(file -> {
                    try {
                        return upload(AttachmentUploadDTO
                                .builder()
                                .ownerType(attachmentUploadsDTO.getOwnerType())
                                .ownerId(attachmentUploadsDTO.getOwnerId())
                                .path(attachmentUploadsDTO.getPath())
                                .file(file)
                                .isPrivate(attachmentUploadsDTO.getIsPrivate())
                                .build()
                        );
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })
                .toList();
    }

    @Override
    public void download(String objName, Consumer<InputStream> consumer) {
        ossService.download(objName, consumer);
    }

    @Override
    public InputStream download(String objName) {
        return ossService.download(objName);
    }

    @Override
    public AttachmentVO queryByObjName(String objName) {
        return BeanUtil.copyProperties(lambdaQuery().eq(Attachment::getObjName, objName).one(), AttachmentVO.class);
    }
}




